#include "hotkeyedit.h"
#include "models/command.h"
#include <sitckcontrol/stickcontrol.h>

#include <QPushButton>
#include <qevent.h>

HotKeyEdit::HotKeyEdit() {
    setReadOnly(true);

    m_nFuncKey = FUNCTION_KEY_NONE;
    m_nHotKey = STICK_KEY_NONE;
    connect(StickControl::getInstance(),&StickControl::keyPressed,this,[&](FUNCTION_KEY nFuncKey, STICK_KEY nKey){
        if(isVisible() && hasFocus())
            setKey(nFuncKey,nKey);
    });
     m_pClearBtn = new TouchButton(this);
    m_pClearBtn->setStyleSheet("  QPushButton{ \
        border-image: url(:/images/delete.png); \
        background-color:transparent; \
        } \
        QPushButton:pressed{ \
        background-color: rgb(255, 0, 0); \
        }");
    m_pClearBtn->setFixedSize(int(40*g_xScale),int(40*g_xScale));
    connect(m_pClearBtn, &QPushButton::clicked,this,[&](){
        setText("");
        m_pClearBtn->hide();
        m_nFuncKey = FUNCTION_KEY_NONE;
        m_nHotKey = STICK_KEY_NONE;
    });
}

void HotKeyEdit::setKey(FUNCTION_KEY nFuncKey, STICK_KEY nHotKey)
{
    m_nFuncKey = nFuncKey;
    m_nHotKey = nHotKey;
    if(m_nHotKey > 0)
    {
        if(m_nFuncKey > 0)
        {
            setText(QString("%1+%2").arg(StickControl::getInstance()->getFuncKeyName(m_nFuncKey)).arg(StickControl::getInstance()->getHotKeyName(m_nHotKey)));
            m_pClearBtn->show();
        }
        else
        {
            setText(QString("%1").arg(StickControl::getInstance()->getHotKeyName(m_nHotKey)));
            m_pClearBtn->show();
        }
    }
    else
    {
        setText("");
        m_pClearBtn->hide();
    }
}

FUNCTION_KEY HotKeyEdit::getFuncKey()
{
    return m_nFuncKey;
}

STICK_KEY HotKeyEdit::getHotKey()
{
    return m_nHotKey;
}

void HotKeyEdit::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    m_pClearBtn->move(width() - m_pClearBtn->width(), (height() - m_pClearBtn->height()) / 2);
}


